package com.lwc.common.crud;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwc.common.bean.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class BaseServiceImpl <T,M extends IMapper>  implements Iservice<T>{

    @Autowired
    public M baseMapper;
    @Override
    public int save(T entity) {
        return baseMapper.save(entity);
    }

    @Override
    public int delete(Serializable id) {
        return baseMapper.delete(id);
    }

    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }

    @Override
    public T query(Serializable id) {
        return(T) baseMapper.query(id);
    }

    //分页
    @Override
    public PageBean query(Map map) {
          int page=1;//默认第一页
       int pageSize=10; //默认10页
        if (null!=map){
            if (map.containsKey("page")){
            page=Integer.parseInt(map.get("page").toString());
            }
            if (map.containsKey("pageSize")){
                pageSize=Integer.parseInt(map.get("pageSize").toString());
            }
        }
        map.forEach((a,b)-> System.out.println(a+""+b));
PageHelper.startPage(page,pageSize);
        List list=baseMapper.query(map);
        PageInfo info=new PageInfo<>(list);
        return new PageBean<>(page,pageSize, info.getTotal(), list);
    }

}
